//
// Created by Administrator on 2022/8/25.
//

#ifndef TPSERVER_TP_STRING_H
#define TPSERVER_TP_STRING_H


#include <stdarg.h>
#include <sys/types.h>
#include <string.h>
#include <stdint.h>

typedef struct {
    size_t      len;
    char     *data;
} tp_str_t;

#define LF     (char) '\n'
#define CR     (char) '\r'
#define CRLF   "\r\n"

#define ATOMIC_T_LEN            (sizeof("-2147483648") - 1)


#define INT32_LEN   (sizeof("-2147483648") - 1)
#define INT64_LEN   (sizeof("-9223372036854775808") - 1)

#define MAX_UINT32_VALUE  (uint32_t) 0xffffffff
#define MAX_INT32_VALUE   (uint32_t) 0x7fffffff

#define INT_T_LEN   INT32_LEN
#define MAX_INT_T_VALUE  2147483647

#define log_max(val1, val2)  ((val1 < val2) ? (val2) : (val1))
#define log_min(val1, val2)  ((val1 > val2) ? (val2) : (val1))

#define tp_memcpy(dst, src, n)   (void) memcpy(dst, src, n)
#define tp_cpymem(dst, src, n)   (((char *) memcpy(dst, src, n)) + (n))

#define tp_string(str)     { sizeof(str) - 1, (char *) str }
#define tp_null_string     { 0, NULL }

uint64_t tp_numlen(uint64_t num);
char *tp_sprintf(char *buf, const char *fmt, ...);
char *tp_snprintf(char *buf, size_t max, const char *fmt, ...);
char *tp_slprintf(char *buf, char *last, const char *fmt, ...);
char *tp_vslprintf(char *buf, char *last, const char *fmt, va_list args);
static char *tp_sprintf_str(char *buf, char *last, char *src, size_t len, int hexadecimal);
static char *tp_sprintf_num(char *buf, char *last, int64_t ui64, char zero, int hexadecimal, int width);

#endif //TPSERVER_TP_STRING_H
